package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${dtoPackage}.${entity}SaveDTO;
import ${dtoPackage}.${entity}QueryDTO;
import ${voPackage}.${entity}VO;
import ${package.Mapper}.${table.mapperName};
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jingyuan.framework.auth.RequestInfo;
import com.jingyuan.framework.auth.RequestInfoContext;
import com.jingyuan.framework.i18n.I18nUtils;
import com.jingyuan.framework.support.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Set;

/**
 * <p>
 * ${table.comment!} 服务类
 * </p>
 * @author ${author}
 * @since ${date}
 */
@Service
@Slf4j
@Transactional
public class ${table.serviceName}  {

    @Autowired
    private ${table.mapperName} ${table.mapperName?uncap_first};


<#list table.fields as field>
<#if field.keyFlag><#--主键java类型--> <#assign keyType="${field.columnType.type}"/><#assign keyName="${field.propertyName}"/></#if>
</#list>

    public void save(${entity}SaveDTO dto) {
        if (dto.getId() == null || dto.getId() <= 0) {
            ${entity} ua = new ${entity}();
            BeanUtils.copyProperties(dto, ua);
            ua.setCreateTime(new Date());
            ${table.mapperName?uncap_first}.insert(ua);
        } else {
            ${entity} ua = ${table.mapperName?uncap_first}.selectById(dto.getId());
            if (ua == null) {
                throw new BizException(I18nUtils.getRequestText("not.exists.data"));
            }
            BeanUtils.copyProperties(dto, ua);
            ua.setUpdateTime(new Date());
            ${table.mapperName?uncap_first}.updateById(ua);
        }
    }
    
    
    public void delete(Set<Integer> ids) {
        ${table.mapperName?uncap_first}.deleteBatchIds(ids);
    }
    
    public ${entity}VO get(int id) {
        ${entity} entity = ${table.mapperName?uncap_first}.selectById(id);
        if (entity == null) {
            throw new BizException(I18nUtils.getRequestText("not.exists.data"));
        }
        ${entity}VO vo = toVo(entity);
        return vo;
    }
    
    public IPage<${entity}VO> query(${entity}QueryDTO query) {
        Page<${entity}> page = new Page<>(query.getPage(), query.getSize());
        QueryWrapper<${entity}> qw = new QueryWrapper<>();
        query.toQueryMap().entrySet().forEach(kv -> {
            if(kv.getValue()!=null && StringUtils.isNotBlank(kv.getValue().toString())) {
            qw.or().eq(kv.getKey(), kv.getValue());
        }
        });
        IPage<${entity}> rep = ${table.mapperName?uncap_first}.selectPage(page, qw);
        return rep.convert(entity -> toVo(entity)); 
    }

    
    private ${entity}VO toVo(${entity} entity) {
        if (entity == null) {
            return null;
        }
        ${entity}VO vo = new ${entity}VO();
        BeanUtils.copyProperties(entity, vo);
      
        return vo;
    }
    
}

